<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学号" prop="studentId">
        <el-input
          v-model="queryParams.studentId"
          placeholder="学生学号"
          clearable
          @keyup.enter="handleQuery"
          style="width: 100px;"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="姓名"
          clearable
          @keyup.enter="handleQuery"
          style="width: 100px;"
        />
      </el-form-item>

      <el-form-item label="官网报名" prop="isOfficialRegistration">
        <el-select
          v-model="queryParams.isOfficialRegistration"
          placeholder="请选择官网报名状态"
          clearable
          @change="handleQuery"
        >
          <el-option label="否" value="0" />
          <el-option label="是" value="1" />
        </el-select>
      </el-form-item>
      
      <!-- 竞赛名称 -->
    <!--   <el-form-item label="竞赛名称" prop="competitionName">
        <el-input
          v-model="queryParams.competitionName"
          placeholder="请输入竞赛名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- 赛道名称 -->
      <el-form-item label="赛道名称" prop="racewayName">
        <el-select
          v-model="queryParams.racewayName"
          placeholder="请选择赛道"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="track in trackList"
            :key="track.id"
            :label="track.trackName"
            :value="track.trackName"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:individualregistration:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:individualregistration:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:individualregistration:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:individualregistration:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="individualregistrationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="唯一标识ID" align="center" prop="id" /> -->
      <el-table-column label="姓名" align="center" prop="studentName" />
      <el-table-column label="学生学号" align="center" prop="studentId" />
      <el-table-column label="是否在官网进行报名" align="center" prop="isOfficialRegistration">
        <template #default="scope">
          <span>{{ scope.row.isOfficialRegistration === '0' ? '否' : scope.row.isOfficialRegistration === '1' ? '是' : '' }}</span>
        </template>
      </el-table-column>
     <!--  <el-table-column label="个人赛记录表id" align="center" prop="individualContestId" /> -->
      <!-- 竞赛名称 -->
    <!--   <el-table-column label="竞赛名称" align="center" prop="competitionName" /> -->
      <!-- 赛道名称 -->
      <el-table-column label="赛道名称" align="center" prop="racewayName" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:individualregistration:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:individualregistration:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改个人赛报名对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="individualregistrationRef" :model="form" :rules="rules" label-width="80px">

       <!--   <el-form-item label="个人赛id" prop="individualContestId">
          <el-input v-model="form.individualContestId" placeholder="请输入个人赛记录表id"  disabled/>
        </el-form-item> -->
        
        <el-form-item label="学生学号" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学生学号" />
        </el-form-item>
       
        <el-form-item label="姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入姓名" />
        </el-form-item>

        <el-form-item label="是否报名" prop="isOfficialRegistration">
          <el-input v-model="form.isOfficialRegistration" placeholder="请输入是否在官网进行报名(0否,1是)" />
        </el-form-item>
       
        <!-- 竞赛名称 -->
        <el-form-item label="竞赛名称" prop="competitionName">
          <el-input v-model="form.competitionName" placeholder="请输入竞赛名称"  disabled/>
        </el-form-item>
        <!-- 赛道名称 -->
        <el-form-item label="赛道名称" prop="racewayName">
          <el-select v-model="form.racewayName" placeholder="请选择赛道名称" @change="handleTrackChange">
            <el-option
              v-for="track in trackList"
              :key="track.id"
              :label="track.trackName"
              :value="track.trackName"
            />
          </el-select>
        </el-form-item>


      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Individualregistration">
import { listIndividualregistration, getIndividualregistration, delIndividualregistration, addIndividualregistration, updateIndividualregistration } from "@/api/competitionmanagement/individualregistration";
import { listIndividualrecord } from "@/api/competitionmanagement/individualrecord";
import {onMounted} from 'vue'


// 定义props
const props = defineProps({
  competitionName: {
    type: String,
    default: ''
  },
  competitionId: {
    type: Number,
    default: null
  }
});

const { proxy } = getCurrentInstance();

const individualregistrationList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const trackList = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    studentId: null,
    isOfficialRegistration: null,
    individualContestId: null,
    competitionName: props.competitionName,
    competitionId: null,
    racewayName: null
  },
  rules: {
    studentId: [
      { required: true, message: "学生学号不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询个人赛报名列表 */
function getList() {
  loading.value = true;
  listIndividualregistration(queryParams.value).then(response => {
    individualregistrationList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询赛道列表 */
function getTrackList() {
  listIndividualrecord({competitionId: props.competitionId}).then(response => {

    // 从个人赛记录中提取唯一的赛道信息
    const uniqueTracks = [];
    const trackNames = new Set();
    
    response.rows.forEach(record => {
      if (!trackNames.has(record.trackName)) {
        trackNames.add(record.trackName);
        uniqueTracks.push({
          id: record.id,
          trackName: record.trackName,
          trackDescription: record.trackDescription
        });
      }
    });
    
    trackList.value = uniqueTracks;
  });
}

/** 处理赛道选择变化 */
function handleTrackChange(trackName) {
  const selectedTrack = trackList.value.find(track => track.trackName === trackName);
  if (selectedTrack) {
    form.value.individualContestId = selectedTrack.id;
  }
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    studentId: null,
    isOfficialRegistration: null,
    individualContestId: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    competitionName: props.competitionName,
    racewayName: null
  };
  proxy.resetForm("individualregistrationRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getTrackList();
getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  getTrackList();
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  // 获取赛道列表
  getTrackList();
  open.value = true;
  title.value = "添加个人赛报名";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getIndividualregistration(_id).then(response => {
    form.value = response.data;
    // 获取赛道列表
    getTrackList();
    open.value = true;
    title.value = "修改个人赛报名";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["individualregistrationRef"].validate(valid => {
    if (valid) {
      proxy.$modal.loading("正在提交中...");
      if (form.value.id != null) {
        updateIndividualregistration(form.value).then(response => {
          proxy.$modal.closeLoading();
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).catch(() => { proxy.$modal.closeLoading(); });
      } else {
        addIndividualregistration(form.value).then(response => {
          proxy.$modal.closeLoading();
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).catch(() => { proxy.$modal.closeLoading(); });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除个人赛报名编号为"' + _ids + '"的数据项？').then(function() {
    return delIndividualregistration(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/individualregistration/export', {
    ...queryParams.value
  }, `individualregistration_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
  getTrackList();
});
</script>
